package servlet;

import pojo.Function;
import pojo.Log;
import pojo.User;
import service.*;
import service.serviceImpl.*;
import servlet.log.LogServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017-11-25.
 */
public class HomeServlet extends BaseServlet {
    LoginService loginService = null;
    UserService userService=new UserServiceImpl();
    LogService logService=new LogServiceImpl();

    public String home(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        loginService =new LoginServiceImpl();
        HttpSession session;
        session= request.getSession();
        User user= (User) session.getAttribute("userInfo");
        //加载home.jsp页面
        String functionDoc="打开";
        List<Function> function = loginService.getFunction(user.getRoleId(),functionDoc);
        session.setAttribute("functions",function);
        List<Function> functionsFirst = new ArrayList<>();
        List<Function> functionsSecond = new ArrayList<>();
        for (Function fc : function) {
            if (fc.getFunctionDoc().equals("打开")) {
                if (fc.getFunctionId() == 1001) {
                    //一级栏目的菜单
                    session.setAttribute("firstFunction", fc);
                    functionsFirst.add(fc);
                    //一级栏目的功能
                    session.setAttribute("functionFirst", functionsFirst);
                } else if (fc.getFunctionId() == 1002) {
                    //二级栏目的菜单
                    session.setAttribute("secondFunction", fc);
                    functionsSecond.add(fc);
                    //二级栏目的功能
                    session.setAttribute("functionSecond", functionsSecond);
                }
            }
        }

        //取出所有的功能说明

        //如果是员工，则取出自己的日志数据


        //如果是部长，则取出部分审核数据
        if(user.getRoleId()==1) {
            LogService logService=new LogServiceImpl();
            String logStatus = "正在上报";
            String department = user.getDepartment();
            List<Log> logs = logService.checkAllLog(logStatus, department);
            request.setAttribute("logs", logs);
            return "jsp/home.jsp";
        }
        //如果是经理，则取出审核日志数据和员工信息
        if(user.getRoleId()==2) {

            List<Log> logs1 = logService.selectAllLog();
            request.setAttribute("logs1", logs1);
            List<User> userList = userService.getAllUser();
            request.setAttribute("userList", userList);
            return "jsp/home.jsp";
        }
        if(user.getRoleId()==3) {
            //如果是管理员，则取出所有文章和所有员工信息
            List<Log> logs2 = logService.selectAllLog();
                request.setAttribute("logs2", logs2);
                List<User> userList1 = userService.getAllUser();
                request.setAttribute("userList1", userList1);
                return "jsp/home.jsp";
        }
            return "jsp/home.jsp";
    }
}
